package demo.thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class TaskInfo<E> {

    private String name;
    private CountDownLatch countDownLatch;
    private List<E> resultList;

    public TaskInfo(String name, int totalCount) {
        this.name = name;
        this.countDownLatch = new CountDownLatch(totalCount);
        // 会多线程并发添加结果，需要使用线程安全List
        this.resultList = Collections.synchronizedList(new ArrayList<>());
    }

    public void addResult(E result) {
        resultList.add(result);
    }

    public void countDown() {
        this.countDownLatch.countDown();
    }

    public void await() throws Exception {
        this.countDownLatch.await();
    }

    public void await(long timeout, TimeUnit timeUnit) throws Exception {
        this.countDownLatch.await(timeout, timeUnit);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    public List<E> getResultList() {
        return resultList;
    }

    public void setResultList(List<E> resultList) {
        this.resultList = resultList;
    }
}
